package com.wangjiang.mario.admin.system.domain.util;

import com.wangjiang.mario.admin.system.object.user.UserRoleMenuVo;
import com.wangjiang.mario.admin.system.object.user.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;

import java.util.HashMap;
import java.util.Map;

/**
 * Shiro工具类
 *
 * @author cuip
 */
@Slf4j
public class ShiroUtils {

    private ShiroUtils() {
    }


    public static UserRoleMenuVo getCurrentUser() {
        PrincipalCollection principalCollection = getSubject().getPrincipals();
        Map principal = (HashMap) principalCollection.getPrimaryPrincipal();
        UserRoleMenuVo user = (UserRoleMenuVo) principal.get("user");
        return user;
    }

    /**
     * 这里切换身份 是有问题的
     * @param user
     * @param token
     */
    public static void setCurrentUser(UserRoleMenuVo user, String token) {
        Subject subject = getSubject();
        PrincipalCollection principalCollection = subject.getPrincipals();
        String realmName = principalCollection.getRealmNames().iterator().next();
        Map map = new HashMap();
        map.put("user", user);
        map.put("payload", token);
        PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(map, realmName);
        // 重新加载Principal
        subject.runAs(newPrincipalCollection);
    }

    /**
     * 获取当前主体
     *
     * @return
     */
    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    /**
     * 获取当前用户session
     *
     * @return
     */
    public static Session getSession() {
        return getSubject().getSession();
    }

    public static String getIp()
    {
        return getSubject().getSession().getHost();
    }

    /**
     * 获取当前登录对象
     *
     * @return
     */
    public static UserVo getUserVo() {
        HashMap subject = (HashMap) ShiroUtils.getSubject().getPrincipal();
        if (null == subject) {
            UserVo user = new UserVo();
            user.setId(-1L);
            user.setUuid("nobody");
            user.setLoginName("定时任务");
            return user;
        }

        return (UserVo) subject.get("user");
    }


    /**
     * 获取当前登录用户的用户名
     *
     * @return
     */
    public static String getUserName() {
        return getUserVo().getLoginName();
    }

    /**
     * 获取当前登录用户的用户ID
     *
     * @return
     */
    public static Long getUserId() {
        return getUserVo().getId();
    }


    /**
     * 获取当前登录用户的用户ID
     *
     * @return
     */
    public static String getUserUUid() {
        return getUserVo().getUuid();
    }


    /**
     * 当前登录用户session赋值
     *
     * @param key
     * @param value
     */
    public static void setSessionAttribute(Object key, Object value) {
        getSession().setAttribute(key, value);
    }

    /**
     * 当前登录用户session取值
     *
     * @param key
     * @return
     */
    public static Object getSessionAttribute(Object key) {
        return getSession().getAttribute(key);
    }

    /**
     * 判断是否登录
     *
     * @return
     */
    public static boolean isLogin() {
        return getSubject().getPrincipal() != null && getSubject().isAuthenticated();
    }

}